package com.hilaver.dzmis.service.impl;

import java.util.Date;
import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.dao.SysMboxDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.sys.SysMbox;
import com.hilaver.dzmis.sys.SysMboxViewer;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class SysMboxServiceImpl extends AbstractBaseService implements
		IEntityService {

	private SysMboxDAO dao;

	public SysMboxServiceImpl() {
		this.dao = new SysMboxDAO();
	}

	public String editSysMboxViewers(SysMboxViewer[] smvs) throws Exception{
		Date createDate = new Date();
		for(SysMboxViewer smv : smvs){
			if(smv.getId() == null){
				if(smv.getReceiver().getId().equals(this.acUser.getId())){
					continue;
				}
				smv.setSender(this.acUser);
				smv.setCreateDate(createDate);
				smv.setIsFinished("02");
			}else{
				SysMboxViewer old = (SysMboxViewer)this.dao.get(SysMboxViewer.class.getName(), smv.getId());
				smv.setCreateDate(old.getCreateDate());
				smv.setSender(old.getSender());
				smv.setReceiver(old.getReceiver());
				if(smv.getIsFinished() == null){
					smv.setIsFinished(old.getIsFinished());
				}
				this.dao.getSession().evict(old);
			}
			smv.setModifyDate(new Date());
			this.dao.saveOrUpdate(smv);
		}
		return "{success: true}";
	}
	
	public String setClosed(Integer id) throws Exception{
		SysMbox sm = (SysMbox)this.dao.get(SysMbox.class.getName(), id);
		if("02".equals(sm.getIsClosed())){
			sm.setIsClosed("01");
		}else{
			sm.setIsClosed("02");
		}
		this.dao.saveOrUpdate(sm);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String get(int id) throws Exception {
		SysMbox mbox = (SysMbox)this.dao.get(SysMbox.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		this.fillMesssage(mbox);
		xmlSB.append("<modifyDate>");
		xmlSB.append(StringUtils.toFullString(mbox.getModifyDate()));
		xmlSB.append("</modifyDate>");
		xmlSB.append(SimpleObj2XML.toXMLFragment(mbox));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<SysMbox> objList = this.dao.getAllPagination(SysMbox.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (SysMbox obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			this.fillMesssage(obj);
			xmlSB.append("<modifyDate>");
			xmlSB.append(StringUtils.toFullString(obj.getModifyDate()));
			xmlSB.append("</modifyDate>");
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(SysMbox.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllUnconfirmedPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<SysMbox> objList = this.dao.getAllUnfirmedPagination(offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (SysMbox obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			this.fillMesssage(obj);
			xmlSB.append("<modifyDate>");
			xmlSB.append(StringUtils.toFullString(obj.getModifyDate()));
			xmlSB.append("</modifyDate>");
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getAllUnfirmedCount(filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	private void fillMesssage(SysMbox mbox) throws Exception{
		Object obj = null, parent = null;
		String message = "";
		obj = this.dao.get(mbox.getCtClassName(), mbox.getObjId());
		if(obj != null){
			message = obj.toString();
		}
		if(mbox.getParentClassName() != null){
			parent = this.dao.get(mbox.getParentClassName(), mbox.getParentObjId());
			message += " (" + parent.toString() + ")";
		}
		if(!StringUtils.isEmpty(message)){
			mbox.setMessage(message);
			this.dao.saveOrUpdate(mbox);
		}
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllViewer(String sort, String order, String[] filters) throws Exception{
		int offset = 0;
		List<SysMboxViewer> smvList = this.dao.getAll(SysMboxViewer.class.getName(), sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (SysMboxViewer obj : smvList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<senderName>");
			xmlSB.append(obj.getSender().getRealName() + "(" + obj.getSender().getUserName() + ")");
			xmlSB.append("</senderName>");
			
			xmlSB.append("<receiverName>");
			xmlSB.append(obj.getReceiver().getRealName() + "(" + obj.getReceiver().getUserName() + ")");
			xmlSB.append("</receiverName>");
			
			xmlSB.append("<createDate>");
			xmlSB.append(StringUtils.toFullString(obj.getCreateDate()));
			xmlSB.append("</createDate>");
			
			xmlSB.append("<createDate>");
			xmlSB.append(StringUtils.toFullString(obj.getCreateDate()));
			xmlSB.append("</createDate>");
			
			xmlSB.append("<modifyDate>");
			xmlSB.append(StringUtils.toFullString(obj.getModifyDate()));
			xmlSB.append("</modifyDate>");
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(1);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(smvList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getViewer(Integer id) throws Exception{
		SysMboxViewer mboxViewer = (SysMboxViewer)this.dao.get(SysMboxViewer.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append("<receiverId>");
		xmlSB.append(mboxViewer.getReceiver().getId());
		xmlSB.append("</receiverId>");
		xmlSB.append("<msgId>");
		xmlSB.append(mboxViewer.getSysMbox().getId());
		xmlSB.append("</msgId>");
		xmlSB.append(SimpleObj2XML.toXMLFragment(mboxViewer));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String deleteViewer(int id) throws Exception {
		this.dao.delete(SysMboxViewer.class.getName(), id);
		return "success";
	}
	
	public String getMyViewerPagination(int page, String sort, String order,
			String[] filters) throws Exception{
		int offset = Constants.PAGE_SIZE * (page - 1);
		
		List<SysMboxViewer> smvList = this.dao.getMyAllPagination(offset, Constants.PAGE_SIZE, sort, order, filters, this.acUser.getId());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (SysMboxViewer obj : smvList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<ctClassName>");
			xmlSB.append(obj.getSysMbox().getCtClassName());
			xmlSB.append("</ctClassName>");
			
			xmlSB.append("<message>");
			xmlSB.append(obj.getSysMbox().getMessage());
			xmlSB.append("</message>");
			
			xmlSB.append("<ctOperation>");
			xmlSB.append(obj.getSysMbox().getCtOperation());
			xmlSB.append("</ctOperation>");
			
			xmlSB.append("<senderName>");
			xmlSB.append(obj.getSender().getRealName() + "(" + obj.getSender().getUserName() + ")");
			xmlSB.append("</senderName>");
			
			xmlSB.append("<receiverName>");
			xmlSB.append(obj.getReceiver().getRealName() + "(" + obj.getReceiver().getUserName() + ")");
			xmlSB.append("</receiverName>");
			
			xmlSB.append("<createDate>");
			xmlSB.append(StringUtils.toFullString(obj.getCreateDate()));
			xmlSB.append("</createDate>");
			
			xmlSB.append("<createDate>");
			xmlSB.append(StringUtils.toFullString(obj.getCreateDate()));
			xmlSB.append("</createDate>");
			
			xmlSB.append("<modifyDate>");
			xmlSB.append(StringUtils.toFullString(obj.getModifyDate()));
			xmlSB.append("</modifyDate>");
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getMyCount(filters, this.acUser.getId()));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

}
